/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package recontec.Classes;

import java.sql.*;
import javax.swing.JOptionPane;
import utilities.MysqlConnect;


/**
 *
 * @author Baltieri
 */
public class Login extends javax.swing.JFrame {
    
            MysqlConnect con_usuario;
            public String user;
    /**
     * Creates new form Login
     */
     public String le_usuario()
    {
        user=tfLogin.getText();
        String usuario = user;
        return usuario;
    }
     
    public Login() {
        initComponents();
        con_usuario = new MysqlConnect();
       
       
    }
    

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        tfLogin = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        jSeparator2 = new javax.swing.JSeparator();
        jLabel4 = new javax.swing.JLabel();
        tfSenha = new javax.swing.JPasswordField();
        jButton2 = new javax.swing.JButton();
        labelback = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Recontec");
        setLocation(new java.awt.Point(5, 22));
        setMinimumSize(new java.awt.Dimension(491, 293));
        setResizable(false);
        getContentPane().setLayout(null);

        jButton1.setText("Entrar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton1);
        jButton1.setBounds(307, 188, 81, 29);

        jLabel3.setForeground(new java.awt.Color(255, 255, 255));
        jLabel3.setText("Senha");
        getContentPane().add(jLabel3);
        jLabel3.setBounds(144, 126, 37, 16);

        tfLogin.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                tfLoginFocusLost(evt);
            }
        });
        getContentPane().add(tfLogin);
        tfLogin.setBounds(144, 86, 244, 28);

        jLabel2.setForeground(new java.awt.Color(255, 255, 255));
        jLabel2.setText("Usuario");
        getContentPane().add(jLabel2);
        jLabel2.setBounds(144, 64, 48, 16);

        jLabel1.setFont(new java.awt.Font("Lucida Grande", 0, 18)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(255, 255, 255));
        jLabel1.setText("RECOMTEC");
        getContentPane().add(jLabel1);
        jLabel1.setBounds(33, 6, 150, 30);
        getContentPane().add(jSeparator1);
        jSeparator1.setBounds(75, 42, 367, 10);
        getContentPane().add(jSeparator2);
        jSeparator2.setBounds(75, 235, 367, 12);

        jLabel4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/recontec/Login Manager.png"))); // NOI18N
        getContentPane().add(jLabel4);
        jLabel4.setBounds(19, 54, 120, 170);
        getContentPane().add(tfSenha);
        tfSenha.setBounds(144, 148, 244, 28);

        jButton2.setText("Cancelar");
        jButton2.setPreferredSize(new java.awt.Dimension(81, 29));
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton2);
        jButton2.setBounds(220, 188, 81, 29);

        labelback.setIcon(new javax.swing.ImageIcon(getClass().getResource("/recontec/background copy.jpg"))); // NOI18N
        getContentPane().add(labelback);
        labelback.setBounds(0, 0, 500, 290);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
       
        if (tfLogin.getText().equals("") || tfSenha.getText().equals(""))
        {
            JOptionPane.showMessageDialog(null, "Os campos nao podem ser vazios");
        }
        else
        {
            le_usuario();
            try
            {
                con_usuario.conecta();
                
                
               String sql = "select * from Funcionarios where Login like'"+
                       tfLogin.getText()+"'and senha like'"+
                       tfSenha.getText()+"'";
               con_usuario.executeSQL(sql);
            
               if (con_usuario.resultset.first())
               {
                   //JOptionPane.showMessageDialog(null, "Hello Good Work");
                   //this.setVisible(false);
                   //this.dispose();
                   new Principal().setVisible(true);
                   this.dispose();
                  //JOptionPane.showMessageDialog(null,user);
               }
               else
               {
                   JOptionPane.showMessageDialog(null, "Usuário ou Senha inválidos");
               }
                 
            }
            catch(SQLException erro)
                    {
                        JOptionPane.showMessageDialog(null, "Erro de SQL");
                    }
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed

  //new Funcionario().setVisible(true);
  //Login.this.dispose();
System.exit(0);        
    }//GEN-LAST:event_jButton2ActionPerformed

    private void tfLoginFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_tfLoginFocusLost
        // TODO add your handling code here:
        le_usuario();
       
    }//GEN-LAST:event_tfLoginFocusLost

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Login().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JLabel labelback;
    private javax.swing.JTextField tfLogin;
    private javax.swing.JPasswordField tfSenha;
    // End of variables declaration//GEN-END:variables
}
